package cn.kbblog.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.beetl.sql.core.SQLManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.kbblog.bean.CommonResult;
import cn.kbblog.biz.DepartBiz;
import cn.kbblog.biz.UserinfoBiz;
import cn.kbblog.entity.Userinfo;

@Controller
@RequestMapping("/main")
public class MainController {
	
	@Resource
	private UserinfoBiz userinfoBiz;
	@Resource
	private DepartBiz departBiz;
	
	@Autowired
	private SQLManager sqlManager;
	
	private static Logger LOG = LoggerFactory.getLogger(MainController.class);
	
	@GetMapping("/index")
	public String main(Model model){
		return "main";
	}
	
	@RequestMapping(value = "/list",method=RequestMethod.POST)
	@ResponseBody
	public CommonResult list() {
		try {
//			List<Userinfo> list = sqlManager.execute("select * from userinfo", Userinfo.class,null);
			Map<String,Object> map = new HashMap<>();
			map.put("id", 1);
			List<Userinfo> list = sqlManager.execute("select * from userinfo where id = #id#", Userinfo.class,map);
			return CommonResult.resultSuccess(list, list.size());
		} catch (Exception e) {
			return CommonResult.resultFail(e,LOG);
		}
	}
	
	/**
	 * 测试点对点消息队列
	* @param userinfo
	* @return
	* CommonResult
	* @author ykb yang.kb@topcheer.com   
	* @date 2017年5月31日 上午9:51:27
	 */
	@RequestMapping(value = "/addQueue",method=RequestMethod.POST)
	@ResponseBody
	public CommonResult addQueue(Userinfo userinfo) {
		try {
				userinfoBiz.addUserinfo(userinfo);
				return CommonResult.resultSuccess("保存成功");
		} catch (Exception e) {
			e.printStackTrace();
			return CommonResult.resultFail(e,LOG);
		}
	}
	
	/**
	 * 测试订阅消息
	* @param message
	* @return
	* CommonResult
	* @author ykb yang.kb@topcheer.com   
	* @date 2017年5月31日 上午9:55:59
	 */
	@RequestMapping(value = "/addTopic",method=RequestMethod.POST)
	@ResponseBody
	public CommonResult addTopic(String message) {
		try {
			userinfoBiz.addTopic(message);
			return CommonResult.resultSuccess("订阅成功");
		} catch (Exception e) {
			e.printStackTrace();
			return CommonResult.resultFail(e,LOG);
		}
	}
	
	@RequestMapping(value = "/departPage",method=RequestMethod.POST)
	@ResponseBody
	public CommonResult getDepartPage() {
		try {
			departBiz.getDepartPage();
			return CommonResult.resultSuccess("获取成功");
		} catch (Exception e) {
			e.printStackTrace();
			return CommonResult.resultFail(e,LOG);
		}
	}
	@RequestMapping(value = "/saveDepart",method=RequestMethod.POST)
	@ResponseBody
	public CommonResult saveDepart() {
		try {
			departBiz.saveDepart();
			return CommonResult.resultSuccess("添加成功");
		} catch (Exception e) {
			e.printStackTrace();
			return CommonResult.resultFail(e,LOG);
		}
	}
	
}
